/** 
 * 
 * 开场动画相机, 相机有一个按钮.
 * 按下这个按钮会吐出照片...
 * 需求变简单了... 按一下按钮吐一张
 * 
 */
class Camera extends egret.DisplayObjectContainer {
    /** 加载相机图片资源 */
    private camera_png: egret.Bitmap = Utils.createBitmapByName("camera_png");
    /** 加载按钮图片资源 */
    private btn_png: egret.Bitmap = Utils.createBitmapByName("camera_btn_png");
    /** 加载提示图片资源 */
    public tip_png: egret.Bitmap = Utils.createBitmapByName("tip_png");
    /** 加载按键音资源 */
    public btn_sound: egret.Sound = RES.getRes("clickBtn_mp3");
    public channel = null;

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        /** 把相机绘制到容器中 */
        this.addChild(this.camera_png);
        /** 把按钮绘制到容器中 */
        this.addChild(this.btn_png);
        Utils.anchorCenter(this.btn_png);
        /** 把提示绘制到容器中 */
        this.addChild(this.tip_png);

        this.anchorOffsetY = this.camera_png.height;
        this.y = this.stage.stageHeight;

        this.btn_png.x = 110;
        this.btn_png.y = this.camera_png.height - this.btn_png.height - 142;
        this.btn_png.touchEnabled = true;
        this.btn_png.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.btnTouchStart, this);
        this.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.btnTouchEnd, this);

        this.tip_png.anchorOffsetY = this.tip_png.height;
        this.tip_png.x = this.btn_png.x + 50;
        this.tip_png.y = this.btn_png.y - 10;


        egret.Tween.get(this.tip_png, { loop: true })
            .to({ rotation: 10 }, 1000)
            .to({ rotation: 0 }, 1000)
            .to({ rotation: -10 }, 1000)
            .to({ rotation: 0 }, 1000);

    }

    private btnTouchStart() {
        this.channel = this.btn_sound.play(0, 1);
        this.btn_png.scaleX = .9;
        this.btn_png.scaleY = .9;
        this.tip_png.visible = false;
        this.dispatchEvent(new egret.Event("camera_btn_down"));
    }

    private btnTouchEnd() {
        this.btn_png.scaleX = 1;
        this.btn_png.scaleY = 1;
        this.dispatchEvent(new egret.Event("camera_btn_up"));
    }

}